(function() {

    function main() {

        var acceptedTokenPattern = /You have accepted/g;
        var expiredTokenPattern = /You have already accepted this gift or it has expired/g;
        var invalidTokenPattern = /We were unable to complete your request/g;
        var giftItemPattern = /pf_giftItemId=(\d+)/ig;

        var result = giftItemPattern.exec(document.URL);
        if (result) {

            var request = {
                "itemId": result[1]
            };

            var html = document.documentElement.innerHTML;

            if (acceptedTokenPattern.test(html)) {
                request["status"] = "accepted";
            } else if (expiredTokenPattern.test(html)) {
                request["status"] = "expired";
            } else if (invalidTokenPattern.test(html)) {
                request["status"] = "invalid";
            }

            chrome.extension.sendRequest(request);
        }
    }

    main();

})();